/*
 * @Description:
 * @Author: 阳婷
 * @Date: 2019-07-30 10:21:49
 * @LastEditTime: 2022-04-01 14:06:40
 * @LastEditors: 阳婷
 */
/**
 * 黄龙
 * @method Ajax 方法
 * @param TYPE  请求方式
 * @param URL   请求url
 * @param DATA  请求参数
 * @param FN    返回方法
 */
import { ElMessage } from "element-plus";
import axios from "axios";
import Qs from "qs";
import router from "@/router";
import NProgress from "nprogress"; // Progress 进度条
import "nprogress/nprogress.css"; // Progress 进度条样式

export default function $ajax(TYPE, URL, DATA, FN) {
  NProgress.start();
  //process.env.VUE_APP_BASE_URL
  //+ "?access-token=" + getToken();
  // let url = process.env.VUE_APP_BASE_URL + URL;
  let url = process.env.VUE_APP_BASE_URL + URL;
  if (TYPE.toLowerCase() == "get") {
    GetAjax(url, DATA, FN);
  } else if (TYPE.toLowerCase() == "post") {
    PostAjax(url, DATA, FN);
  } else if (TYPE.toLowerCase() == "put") {
    PutAjax(url, DATA, FN);
  } else if (TYPE.toLowerCase() == "delete") {
    DeleteAjax(url, DATA, FN);
  }
}

export function newUpload(URL, data) {
  let url =process.env.VUE_APP_BASE_URL + URL;
  return axios({
    method: "post",
    url: url,
    headers: {
      token: sessionStorage.getItem("token_autoSale"),
      // Authorization: "Bearer " + sessionStorage.getItem("token_autoSale")
    },
    timeout: 20000,
    data: data,
  });
}
/*
 *  post请求
 *  URL:请求地址
 *  DATA:参数
 *  FN:回调函数
 * */
function PostAjax(URL, DATA, FN) {
  axios
    .post(URL, JSON.stringify(DATA), {
      headers: {
        token: sessionStorage.getItem("token_autoSale"),
        "Content-Type": "application/json",
        // Authorization: "Bearer " + sessionStorage.getItem("token_autoSale"),
      },
    })
    .then((response) => {
      Judge(response.data, FN);
    });
}
/*
 *  put请求 修改
 *  URL:请求地址
 *  DATA:参数
 *  FN:回调函数
 * */
function PutAjax(URL, DATA, FN) {
  axios
    .put(URL, JSON.stringify(DATA), {
      headers: {
        "Content-Type": "application/json",
        // Authorization: "Bearer " + sessionStorage.getItem("token_autoSale"),
        token: sessionStorage.getItem("token_autoSale"),
      },
    })
    .then((response) => {
      Judge(response.data, FN);
    });
}
/*
 *  delete请求
 *  URL:请求地址
 *  DATA:参数
 *  FN:回调函数
 * */
function DeleteAjax(URL, DATA, FN) {
  axios({
    method: "DELETE",
    url: URL,
    data: JSON.stringify(DATA),
    headers: {
      "Content-Type": "application/json",
      // Authorization: "Bearer " + sessionStorage.getItem("token_autoSale"),
      token: sessionStorage.getItem("token_autoSale"),
    },
  }).then((response) => {
    Judge(response.data, FN);
  });
}
/*
 *  get请求
 *  URL:请求地址
 *  DATA:参数
 *  FN:回调函数
 * */
function GetAjax(URL, DATA, FN) {
  axios
    .get(URL, {
      params: DATA,
      headers: {
        // Authorization: "Bearer " + sessionStorage.getItem("token_autoSale"),
        token: sessionStorage.getItem("token_autoSale"),
        //  "Content-Type": "application/json",
      },
      paramsSerializer: (params) => {
        return Qs.stringify(params, { arrayFormat: "indices" })
        // if (JSON.stringify(params) === '{}') {
        //   return Qs.stringify(params, { arrayFormat: "indices" })
        // } else {
        //   return JSON.stringify(params)
        // }
      }
    })
    .then((response) => {
      Judge(response.data, FN);
    });
}
/*
 *  body:后台返回值
 *  FN:回调函数
 *  code:状态值判断
 * */
function Judge(body, FN) {
  NProgress.done();
  if (body.code == 200) {
    FN(body);
  } else if (body.code == 401) {
    if (router.currentRoute.value.fullPath != "/login") {
      ElMessage({
        showClose: true,
        message: "登录超时，请重新登录",
        type: "error",
      });
      router.push("/login");
    }
    return;
  } else {
    ElMessage({
      showClose: true,
      message: body.msg,
      type: "warning",
    });
  }
}
